<template>
  <div class="login">
    <div class="login-container">
      <div class="login-header">
        <img src="@/assets/logo/logo.png" alt="山海之约" class="login-logo">
        <div class="login-title">点击登录，体验更多功能</div>
      </div>
      
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="请输入账号"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 65%"
            prefix-icon="el-icon-key"
            @keyup.enter.native="handleLogin"
          ></el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        
        <div class="login-options">
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">记住密码</el-checkbox>
          <div class="forgot-password">忘记密码</div>
        </div>
        
        <el-form-item class="login-btn-item">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            class="login-button"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">一键快捷登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-agreement">
        <el-checkbox v-model="agreement" class="agreement-checkbox"></el-checkbox>
        <span class="agreement-text">
          我已阅读并同意
          <span class="link-text">《服务协议》</span>和
          <span class="link-text">《个人信息保护指引》</span>
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      // 用户协议勾选
      agreement: true
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          if (!this.agreement) {
            this.$message.error('请阅读并同意相关协议');
            return;
          }
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("https://img.axureshop.com/a7/c7/6c/a7c76cc84f67407ca767f97a1f789c75/images/shouye/u1.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  
  .login-container {
    width: 100%;
    max-width: 420px;
    padding: 30px 20px;
    border-radius: 12px;
    background: #ffffff;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  }
  
  .login-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 30px;
    
    .login-logo {
      width: 80px;
      height: 80px;
      margin-bottom: 15px;
    }
    
    .login-title {
      font-size: 18px;
      color: #333;
      font-weight: 500;
    }
  }

  .login-form {
    .el-input {
      height: 48px;
      
      input {
        height: 48px;
        background-color: #f7f7f7;
        border: none;
        border-radius: 8px;
        padding-left: 45px;
        color: #333;
        
        &::placeholder {
          color: #999;
        }
      }
      
      .el-input__prefix {
        left: 15px;
        font-size: 18px;
        color: #666;
      }
    }
    
    .el-form-item {
      margin-bottom: 20px;
    }
  }
  
  .login-options {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    
    .remember-me {
      color: #666;
      font-size: 14px;
    }
    
    .forgot-password {
      color: #1989fa;
      font-size: 14px;
      cursor: pointer;
    }
  }
  
  .login-btn-item {
    margin-top: 10px;
  }
  
  .login-button {
    width: 100%;
    height: 48px;
    border-radius: 8px;
    font-size: 16px;
    background-color: #1989fa;
    border-color: #1989fa;
    
    &:hover, &:focus {
      background-color: #0d7fee;
      border-color: #0d7fee;
    }
  }
  
  .login-code {
    width: 32%;
    height: 48px;
    float: right;
    
    .login-code-img {
      height: 48px;
      border-radius: 8px;
      cursor: pointer;
    }
  }
  
  .login-agreement {
    display: flex;
    align-items: center;
    margin-top: 30px;
    font-size: 12px;
    color: #666;
    
    .agreement-text {
      margin-left: 5px;
    }
    
    .link-text {
      color: #1989fa;
      cursor: pointer;
    }
  }
}
</style>
